package com.jzg.jcptadmin.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.MD5Utils;
import com.jzg.jcptadmin.Utils.MyToast;
import com.jzg.jcptadmin.adpter.CityAdapter;
import com.jzg.jcptadmin.adpter.ProvinceAdapter;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.City;
import com.jzg.jcptadmin.data.vo.Province;
import com.jzg.jcptadmin.presenter.AreaPresenter;
import com.jzg.jcptadmin.viewinterface.AreaInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by wangyd on 15/12/25.
 * 地区列表界面
 */
public class AreaActivity extends BaseActivity implements AreaInterface, View.OnClickListener, AdapterView.OnItemClickListener {
    @Bind(R.id.title_return)
    ImageView title_return;
    @Bind(R.id.title_content)
    TextView title_content;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawer_layout;
    @Bind(R.id.province_list)
    ListView province_list;
    @Bind(R.id.city_list)
    ListView city_list;

    private AreaPresenter areaPresenter;
    private ProvinceAdapter provinceAdapter;
    private CityAdapter cityAdapter;
    private List<Province> provinces;
    private List<City> cities;

    private Province curProvince;
    private City curCity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.area_layout);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (areaPresenter != null)
            areaPresenter.detachView();
        ButterKnife.unbind(this);
    }

    private void initView() {
        String title = getIntent().getStringExtra(Constant.KEY_TITLE);
        if(TextUtils.isEmpty(title))
            title = "选择地区";
        title_content.setText(title);
        areaPresenter = new AreaPresenter(DataManager.getInstance());
        areaPresenter.attachView(this);
        areaPresenter.loadProvince();
    }

    /**
     * 获取字体默认颜色
     *
     * @return
     */
    @Override
    public int getDefaultFontColor() {
        return getResources().getColor(R.color.grey3);
    }

    /**
     * 显示省列表
     *
     * @param provinces
     */
    @Override
    public void showProvince(ArrayList<Province> provinces) {
        this.provinces = provinces;
        provinceAdapter = new ProvinceAdapter(this,
                this.provinces);
        province_list.setAdapter(provinceAdapter);
    }

    /**
     * 显示市列表
     *
     * @param cities
     */
    @Override
    public void showCity(List<City> cities) {
        if (!drawer_layout.isDrawerOpen(city_list))//6562908sun
            drawer_layout.openDrawer(city_list);
//        cities.remove(0);
        this.cities = cities;
        cityAdapter = new CityAdapter(this, cities);
        city_list.setAdapter(cityAdapter);
    }

    /**
     * 获取省数据参数
     *
     * @return
     */
    @Override
    public Map<String, String> getProvinceParams() {

        Map<String, Object> signMaps = new HashMap<String, Object>();
        signMaps.put("op", "GetProvJson");
        signMaps.put("userId", appContext.getUser().getUserId() + "");
        signMaps.put("tokenid", "6");
        String sign = MD5Utils.getMD5Sign(signMaps, "2CB3147B-D93C-964B-47AE-EEE448C84E3C");

        Map<String, String> params = new HashMap<String, String>();
        params.put("userId", appContext.getUser().getUserId() + "");
        params.put("op", "GetProvJson");
        params.put("tokenid", "6");
        params.put("sign", sign);
        return params;

//        Map<String, Object> signs = new HashMap<String, Object>();
//        signs.put("op", "GetProvJson");
//        String sign = MD5Utils.getMD5Sign(signs, "2CB3147B-D93C-964B-47AE-EEE448C84E3C");
////        String sign = MD5Utils.getMD5Sign(signs);
//        Map<String, String> params = new HashMap<String, String>();
//        params.put("op", "GetProvJson");
//        params.put("sign", sign);

    }

    /**
     * 获取市数据参数
     *
     * @return
     */
    @Override
    public Map<String, String> getCityParams() {
        Map<String, Object> signMaps = new HashMap<String, Object>();
        signMaps.put("ProvId", String.valueOf(curProvince.getId()));
        signMaps.put("op", "GetCityJson");
        signMaps.put("userId", appContext.getUser().getUserId() + "");
        signMaps.put("tokenid", "6");
        String sign = MD5Utils.getMD5Sign(signMaps, "2CB3147B-D93C-964B-47AE-EEE448C84E3C");

        Map<String, String> params = new HashMap<String, String>();
        params.put("userId", appContext.getUser().getUserId() + "");
        params.put("ProvId", String.valueOf(curProvince.getId()));
        params.put("op", "GetCityJson");
        params.put("tokenid", "6");
        params.put("sign", sign);
        return params;
    }

    /**
     * 显示错误信息
     *
     * @param error 错误信息
     */
    @Override
    public void showError(String error) {
        MyToast.showShort(Constant.errr_net);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    @OnClick({R.id.title_return})
    public void onClick(View v) {
        if (v.getId() == R.id.title_return) {
//            areaPresenter.clearAreaInfo(appContext);
            finish();
        }

    }

    /**
     * Callback method to be invoked when an item in this AdapterView has
     * been clicked.
     * <p/>
     * Implementers can call getItemAtPosition(position) if they need
     * to access the data associated with the selected item.
     *
     * @param parent   The AdapterView where the click happened.
     * @param view     The view within the AdapterView that was clicked (this
     *                 will be a view provided by the adapter)
     * @param position The position of the view in the adapter.
     * @param id       The row id of the item that was clicked.
     */
    @Override
    @OnItemClick({R.id.province_list, R.id.city_list})
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int viewid = parent.getId();
        switch (viewid) {
            case R.id.province_list:
//                Logger.i(String.valueOf(provinces.get(position)));
                curProvince = provinces.get(position);
                areaPresenter.loadCity();
                break;
            case R.id.city_list:
//                Logger.i("cities.get(position) is " + cities.get(position));
                curCity = cities.get(position);
                sendSelectInfoBroadcast();
                finish();
                break;
        }
    }

    /**
     * 发送城市信息广告
     */
    private void sendSelectInfoBroadcast() {
        Intent intent = new Intent();
        intent.setAction(Constant.CITY_SELECT_BROADCAST);
        intent.putExtra(Constant.PROVINCE, (Parcelable) curProvince);
        intent.putExtra(Constant.CITY, curCity);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}
